<?php 
/*
 module:		文章
 create_time:	2024-03-13 09:01:13
 author:		
 contact:		
*/

namespace app\api\controller;

use app\api\model\ArticleApply;
use app\api\model\ArticleCategory;
use app\api\service\ArticleService;
use app\api\model\Article as ArticleModel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;

class Article extends Common {


	/**
	* @api {get} /Article/index 01、首页数据列表
	* @apiGroup Article
	* @apiVersion 1.0.0
	* @apiDescription  首页数据列表
	* @apiParam (输入参数：) {int}     		[limit] 每页数据条数（默认20）
	* @apiParam (输入参数：) {int}     		[page] 当前页码
	* @apiParam (输入参数：) {string}		[type_id] type_id 
	* @apiParam (输入参数：) {string}		[parentid] parentid 
	* @apiParam (输入参数：) {string}		[title] title 
	* @apiParam (输入参数：) {string}		[content] content 
	* @apiParam (输入参数：) {string}		[tit_color] tit_color 
	* @apiParam (输入参数：) {string}		[tit_b] tit_b 
	* @apiParam (输入参数：) {string}		[small_img] small_img 
	* @apiParam (输入参数：) {string}		[author] author 
	* @apiParam (输入参数：) {string}		[source] source 
	* @apiParam (输入参数：) {string}		[focos] focos 
	* @apiParam (输入参数：) {string}		[is_display] is_display 
	* @apiParam (输入参数：) {string}		[is_url] is_url 
	* @apiParam (输入参数：) {string}		[seo_keywords] seo_keywords 
	* @apiParam (输入参数：) {string}		[seo_description] seo_description 
	* @apiParam (输入参数：) {string}		[click] click 
	* @apiParam (输入参数：) {string}		[addtime] addtime 
	* @apiParam (输入参数：) {string}		[article_order] article_order 
	* @apiParam (输入参数：) {string}		[robot] robot 
	* @apiParam (输入参数：) {string}		[subsite_id] 分站id 
	* @apiParam (输入参数：) {string}		[rs_id] 人社那边的id 

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据
	* @apiParam (成功返回参数：) {string}     	array.data.list 返回数据列表
	* @apiParam (成功返回参数：) {string}     	array.data.count 返回数据总数
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" 201","msg":"查询失败"}
	*/
	function index(){
		$limit  = $this->request->get('limit', 20, 'intval');
		$page   = $this->request->get('page', 1, 'intval');

		$where = [];
		$where['type_id'] = $this->request->get('type_id', '', 'serach_in');
		$where['parentid'] = $this->request->get('parentid', '', 'serach_in');
		$where['title'] = $this->request->get('title', '', 'serach_in');
		$where['content'] = $this->request->get('content', '', 'serach_in');
		$where['tit_color'] = $this->request->get('tit_color', '', 'serach_in');
		$where['tit_b'] = $this->request->get('tit_b', '', 'serach_in');
		$where['small_img'] = $this->request->get('small_img', '', 'serach_in');
		$where['author'] = $this->request->get('author', '', 'serach_in');
		$where['source'] = $this->request->get('source', '', 'serach_in');
		$where['focos'] = $this->request->get('focos', '', 'serach_in');
		$where['is_display'] = $this->request->get('is_display', '', 'serach_in');
		$where['is_url'] = $this->request->get('is_url', '', 'serach_in');
		$where['seo_keywords'] = $this->request->get('seo_keywords', '', 'serach_in');
		$where['seo_description'] = $this->request->get('seo_description', '', 'serach_in');
		$where['click'] = $this->request->get('click', '', 'serach_in');
		$where['addtime'] = $this->request->get('addtime', '', 'serach_in');
		$where['article_order'] = $this->request->get('article_order', '', 'serach_in');
		$where['robot'] = $this->request->get('robot', '', 'serach_in');
		$where['subsite_id'] = $this->request->get('subsite_id', '', 'serach_in');
		$where['rs_id'] = $this->request->get('rs_id', '', 'serach_in');

		$field = '*';
		$orderby = 'id desc';

		$res = ArticleService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
        foreach ($res['list'] as &$v){
            $v['other_detail'] = json_decode(htmlspecialchars_decode($v['other_detail']),true);
        }
		return $this->ajaxReturn($this->successCode,'返回成功',htmlOutList($res));
	}

    public function getNextCate()
    {
        $type_id = $this->request->get('type_id', 0, 'intval');
        $data = ArticleCategory::where(['parentid' => $type_id])->select();
        return $this->ajaxReturn($this->successCode,'下级分类',htmlOutList($data));
    }


    public function detail()
    {
        $article_id = $this->request->get('article_id', 0, 'intval');
        \app\api\model\Article::where(['id' => $article_id])->inc('click', 1)->update();
        $data = \app\api\model\Article::where(['id' => $article_id])->find()->toArray();
        $data['other_detail'] = json_decode(htmlspecialchars_decode($data['other_detail']),true);
        return $this->ajaxReturn($this->successCode,'文章详情',htmlOutList($data));
    }

    public function apply()
    {
        $uid = intval($this->request->uid);
        $name = $this->request->post('name', 0);
        $mobile = $this->request->post('mobile', 0);
        $idcard = $this->request->post('idcard', 0);
        $article_id = $this->request->post('article_id', 0);
        if(empty($name) || empty($mobile) || empty($idcard) || empty($article_id)){
            return $this->ajaxReturn($this->errorCode,'信息缺失',htmlOutList([]));
        }
        $where = [
            'user_id' => $uid,
            'article_id' => $article_id
        ];
        $count = ArticleApply::where($where)->count();
        if($count){
            //return $this->ajaxReturn($this->errorCode,'你已经报过名了',htmlOutList([]));
        }
        $result = ArticleApply::insert([
            'name'=>$name,
            'mobile'=>$mobile,
            'idcard'=>$idcard,
            'article_id'=>$article_id,
            'create_time'=>date('Y-m-d H:i:s'),
            'user_id'=>$uid,
        ]);
        if($result){
            return $this->ajaxReturn($this->successCode,'提交成功',htmlOutList([]));
        }
        return $this->ajaxReturn($this->errorCode,'提交失败',htmlOutList([]));
    }

    public function getMyApply()
    {
        $uid = intval($this->request->uid);
        $article_id = $this->request->get('article_id', 0, 'intval');
        $where = [
            'user_id' => $uid
        ];
        if(!empty($article_id)){
            $where ['article_id']= $article_id;
        }
        $data = ArticleApply::where($where)->order("id desc")->find();
        if($data){
            $data = $data->toArray();
        }else{
            $data = [];
        }
        return $this->ajaxReturn($this->successCode,'我的报名信息',htmlOutList($data));
    }
}

